﻿/**
*	Transformation
*	@author 	Liam O'Donnell 2008-03-28
**/

package com.spikything.pants3d.core {
	
	import com.spikything.pants3d.objects.Sprite3D;
	import com.spikything.pants3d.objects.IRenderable;
	
	public class Transformation {

		public var sx:Number;
		public var cx:Number;
		public var sy:Number;
		public var cy:Number;
		public var sz:Number;
		public var cz:Number;

		public function Transformation (rx:Number, ry:Number, rz:Number) {
			
			var sin:Function = Math.sin;
			var cos:Function = Math.cos;
			
			sx = sin(rx);
			cx = cos(rx);
			sy = sin(ry);
			cy = cos(ry);
			sz = sin(rz);
			cz = cos(rz);
			
		}
		
		// Rotates a point around an origin by embedded settings
		public function transformPoint (point:IRenderable, origin:IRenderable):IRenderable {
			
			var x:Number, y:Number, z:Number;
			var xy:Number, xz:Number;
			var yx:Number, yz:Number;
			var zx:Number, zy:Number;

			x = point.px - origin.px; //point.x ...
			y = point.py - origin.py;
			z = point.pz - origin.pz;

			// rotation around x
			xy = cx * y - sx * z;
			xz = sx * y + cx * z;
			
			// rotation around y
			yz = cy * xz - sy * x;
			yx = sy * xz + cy * x;
			
			// rotation around z
			zx = cz * yx - sz * xy;
			zy = sz * yx + cz * xy;
				
			x = zx;
			y = zy;
			z = yz;
				
			point.px = x;
			point.py = y;
			point.pz = z;
			
			return point;

		}
		
		public function transformPoints (points:Array, origin:IRenderable):Array {
			for (var i:int = 0; i < points.length; i++) points[i] = transformPoint(points[i], origin);
			return points;
		}
		
		/*
		public function transformSprites (sprites:Array, origin:IRenderable):Array {
			for (var i:int = 0; i < sprites.length; i++) {
				var sprite:Sprite3D = sprites[i] as Sprite3D;
				var point:Point3D = new Point3D(sprite.px, sprite.py, sprite.pz);
				transformPoint(point, origin);
				sprite.px = point.x;
				sprite.py = point.y;
				sprite.pz = point.z;
			}
			return sprites;
		}
		*/

	}
	
}